using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;

using ADODB;
using ADOMD;

/// <summary>
/// Summary description for CubeManager
/// </summary>
public class CubeManager {
    private static Connection adoConn = null;
    private static Catalog catalog = null;
    private static CubeDefs cubes = null;
    private static Cellset cellSet = null;
  
    public CubeManager() {
        //
        // TODO: Add constructor logic here
        //
    }
    public static List<string> getCubesFiles() {
        List<string> cubes = new List<string>();
        DirectoryInfo cubesDir = new DirectoryInfo(HttpContext.Current.Request.PhysicalApplicationPath + "\\Cubes");
        FileInfo[] cubeFile = cubesDir.GetFiles();
        foreach (FileInfo cube in cubeFile) {
            if (cube.Extension.Equals(".cub")) {
                cubes.Add(cube.Name);
            }
        }
        return (cubes);
    }
    public static string getCubeDefinition(string cube) {
        string cubeDefinition = String.Empty;
        string strConn = "provider=MSOLAP.2;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + "Cubes\\" + cube + ";";
        adoConn = new Connection();
        catalog = new Catalog();
        try {
            adoConn.Open(strConn, "", "", 0);
            catalog.ActiveConnection = adoConn;
            cubeDefinition = catalog.Name.ToString();
        } catch (Exception sysExc) { } finally {
            adoConn.Close();
            adoConn = null;
            catalog = null;
        }
        return (cubeDefinition);
    }
    public static string getDimensions(string cube) {
        string value = null;
        string strConn = "provider=MSOLAP.2;Data Source=" + HttpContext.Current.Request.PhysicalApplicationPath + "Cubes\\" + cube + ";";
        adoConn = new Connection();
        catalog = new Catalog();
        cellSet = new Cellset();
        int k;
        try {
            adoConn.Open(strConn, "", "", 0);
            catalog.ActiveConnection = adoConn;
            cellSet.ActiveConnection = adoConn;
            cubes = catalog.CubeDefs;
            foreach (CubeDef cubito in cubes) {
                foreach (ADOMD.Dimension dimension in cubito.Dimensions) {
                    value += "<li class=\"closed\"><div id='" + dimension.UniqueName + "' onmouseover=\"this.style.color='#9078FF'\"; onmouseout=\"this.style.color='#333366'\" FieldList dragEnabled dimension>" + dimension.Name + "</div>\n";

                    value += "<ul>\n";
                    foreach (ADOMD.Hierarchy hierarchy in dimension.Hierarchies) {
                        
                        if (hierarchy.UniqueName != "[Measures]") {
                            for (k = 0; k < hierarchy.Levels.Count; k++) {
                                value += "<li style=list-style-image:url(design/imgs/this.gif)><div id='" + hierarchy.Levels[k].UniqueName + "' onmouseover=\"this.style.color='#9078FF'\"; onmouseout=\"this.style.color='#333366'\" FieldList dragEnabled level>" + hierarchy.Levels[k].Caption + "</div></li>\n\n";

                            }
                        } else {
                            for (k = 0; k < hierarchy.Levels[0].Members.Count; k++) {
                                value += "<li style=list-style-image:url(design/images/measure.gif)><a return false;' id='" + hierarchy.Levels[0].Members[k].UniqueName + "'onmouseover=\"this.style.color='#9078FF'\"; onmouseout=\"this.style.color='#333366'\" FieldList dragEnabled member>" + hierarchy.Levels[0].Members[k].Caption + "</a></li>\n";

                            }
                        }
                    }
                    value += "</ul>\n</li>\n";

                }
            }
            value += "</ul>\n</div>\n";

        } catch (Exception sysExc) {
            value = sysExc.Message;
        } finally {
            adoConn.Close();
            adoConn = null;
            catalog = null;
        }
        return (value);
    }
}
